package model.shelves;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.naming.OperationNotSupportedException;

import model.events.RentalAddedEvent;
import model.events.RentalRemovedEvent;
import model.rentals.Rental;
import model.shelves.criteria.ICriterion;

/**
 * Classe SmartShelf cria uma shelf to tipo Smart que filtra automaticamente
 * o conteudo da pasta My Rentals utilizando um criterio de selecao 
 * (icriterion)
 * @author DCO37
 * 	N.º 42484 Helder Ferreira
 *  N.º 45509 Luis Ferreira
 */
public class SmartShelf extends Shelf {
	// lista de Rentals
	private List<Rental> myRent;

	/**
	 * Contrutor SmartShelves 
	 * @param name - nome da shelf
	 * @param myRentals - shelf do user
	 * @param icriterion - criterio de selecao
	 */
	public SmartShelf (String name, Shelf myRentals , ICriterion icriterion) {
		super(myRentals.getName());
		this.myRent = new ArrayList<Rental>();
		for (int k = 0; k < myRent.size(); k++) {
			if( icriterion.satisfies(myRent.get(k)) ){
				try {
					addRental(myRent.get(k));
				} catch (OperationNotSupportedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	@Override
	/**
	 * Iterador de lista myRent
	 */
	public Iterator<Rental> iterator() {
		return myRent.iterator();
	}

	@Override
	/**
	 * Adiciona um rental a lista de myRent
	 */
	public boolean addRental(Rental rental) 
			throws OperationNotSupportedException {
		if (!myRent.contains(rental)){
			myRent.add(rental);
			setChanged();
			notifyObservers(new RentalAddedEvent());
			return true;
		}
		else
			return false;	

	}
	@Override
	/**
	 * Remove um rental de myRent
	 */
	public boolean removeRental(Rental rental) 
			throws OperationNotSupportedException{
		if (!myRent.contains(rental)){
			myRent.remove(rental);
			setChanged();
			notifyObservers(new RentalRemovedEvent());
			return true;
		}
		else
			return false;	
	}

}
